<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\video\logic;
use think\facade\Hook;
use think\DB;
use think\facade\Cache;
class VideoApilogic{

	/**
     * 获取视频信息
     * @param user_id int 用户id
     * @param title string  视频标题
     * @param file_name string  视频源文件名
     * @param description string  视频描述
     * @param cover_url string  封面
     * @param tags string  标签
     * @param play_type int 视频类型
     * @param is_immerse int 是否可沉浸 0否 1是
     * @param is_rotate int 是否可旋转 0否 1是
     * @param file_size int 视频文件大小。单位：字节
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function get_create_upload_video($user_id, $title, $file_name, $description, $cover_url, $tags, $play_type, $is_immerse, $is_rotate, $file_size, $video_classid, $class_tage_array, $is_original){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id'           => $user_id,
                'title'             => $title,
                'file_name'         => $file_name,
                'description'       => $description,
                'cover_url'         => $cover_url,
                'tags'              => $tags,
                'play_type'         => $play_type,
                'is_immerse'        => $is_immerse,
                'is_rotate'         => $is_rotate,
                'file_size'         => $file_size,
                'video_classid'     => $video_classid,
                'class_tage_array'  => $class_tage_array,
                'is_original'       => $is_original,
            ];
            /********行为参数赋值（*）********/
        
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            //添加视频表数据
            $result = controller('Vodlogic', 'logic') -> createUploadVideoRequest($title, $file_name, $description, $cover_url, $tags, $file_size, $play_type);
            if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, $result['message']);//逻辑错误 - 1005
            }    
            //添加阿里云接口（最后处理）
            $video_id = $result['data']['VideoId'];
            //$class_tage = implode(',', $class_tage_array);
            $res1 = model('VideoPlay')->addData($user_id, $video_id, $title, $sub_title ='', $description, $play_type, $is_immerse, $is_rotate, $cover_url, $file_size, $video_classid, $class_tage= '', $is_original);
            if($res1['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error2');//逻辑错误 - 1005
            }

            //添加标签关系表
            $data = [];
            foreach ($class_tage_array as $key => $value) { 
                if($value['id'] <= 0){
                    //添加标签
                    $rel_info = model('video/VideoPlayClassLabel')-> addDataTage($value['title']);
                    $label_id = $rel_info['data'];
                }else{
                    $label_id = $value['id'];
                }
                $data[] =[
                    'play_id'  => $res1['data'],
                    'label_id' => $label_id,
                    'create_time' => time(),
                    'update_time' => time(),
                ];
            }//dump($data);die;
            //file_put_contents('VideoPlayLabelRelation.text', '成功：'.json_encode($data).PHP_EOL, FILE_APPEND);
            $res2 = model('VideoPlayLabelRelation') -> insertAll($data);
            if(!$res2){
                DB::rollback();
                return array_out(-1005, 'error3');//逻辑错误 - 1005
            }
            
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success', (object)$result['data']);
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    /**
     * 提交视频 信息
     * @param user_id int 用户id
     * @param title string  视频标题
     * @param file_name string  视频源文件名
     * @param description string  视频描述
     * @param cover_url string  封面
     * @param tags string  标签
     * @param play_type int 视频类型
     * @param is_immerse int 是否可沉浸 0否 1是
     * @param is_rotate int 是否可旋转 0否 1是
     * @param file_size int 视频文件大小。单位：字节
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function submit_video_info($user_id, $play_id, $son_play_id, $title, $description, $cover_url, $tags, $play_type, $is_immerse, $is_rotate, $video_classid, $class_tage_array, $is_original, $goods_id_array){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id'           => $user_id,
                'title'             => $title,
                'play_id'           => $play_id,
                'son_play_id'       => $son_play_id,
                'description'       => $description,
                'cover_url'         => $cover_url,
                'tags'              => $tags,
                'play_type'         => $play_type,
                'is_immerse'        => $is_immerse,
                'is_rotate'         => $is_rotate,
                'video_classid'     => $video_classid,
                'class_tage_array'  => $class_tage_array,
                'is_original'       => $is_original,
                'goods_id_array'    => $goods_id_array
            ];
            /********行为参数赋值（*）********/
        
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            //$class_tage = implode(',', $class_tage_array);
            if($son_play_id >0){
                //更新数据
                $map = [];
                $map['id'] = $son_play_id;
                $res2 = model('VideoPlay') -> where($map) -> setField('father_play_id', $play_id);
                $res4 = model('VideoPlay') -> where($map) -> setField('play_type', 1);//dump($son_play_id);
                if(!$res2||!$res4){
                    DB::rollback();
                    return array_out(-1005, 'error2');//逻辑错误 - 1005
                }

                //更新视频儿子
                $map = [];
                $map['id'] = $play_id;
                $res5 = model('VideoPlay') -> where($map) -> setField('son_play_id', $son_play_id);
                if(!$res5){
                    DB::rollback();
                    return array_out(-1005, 'error2');//逻辑错误 - 1005
                }
            }
            //更新数据
            $res1 = model('VideoPlay') -> editData($play_id, $title, $description, $cover_url, $play_type, $is_immerse, $is_rotate, $play_status =1, $is_recommend=0, $is_original, $video_classid, $class_tage_array, $play_operation_status=0);
            if($res1['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error2');//逻辑错误 - 1005
            }
            //更新视频类型
            $map = [];
            $map['id'] = $play_id;
            $res3 = model('VideoPlay') -> where($map) -> setField('play_type', $play_type);
            if(!$res3){
                DB::rollback();
                return array_out(-1005, 'error22');//逻辑错误 - 1005
            }
            //更新 阿里云数据
            $videoId = model('VideoPlay') -> find($play_id) -> video_id;
            $rel = controller('Vodlogic', 'logic') -> updateVideoInfo($videoId, $title, $description, $cover_url);
            //dump($videoId);
            if($rel['code'] != 200){
                DB::rollback();
                return array_out(-1006, 'error3');//逻辑错误 - 1005
            }

            //商品
            if(is_array($goods_id_array) && !empty($goods_id_array)){
                foreach ($goods_id_array as $key => $value) {
                    $temp_list[] =[
                        'play_id' => $play_id,
                        'goods_id' => $value,
                    ];
                }
                $rel = model('VideoPlayGoods') -> saveAll($temp_list);
                if(!$rel){
                    DB::rollback();
                    return array_out(-1006, 'error4');//逻辑错误 - 1005
                }
            }

            if($son_play_id){
                $videoId = model('VideoPlay') -> find($son_play_id) -> video_id;
                $rel = controller('Vodlogic', 'logic') -> updateVideoInfo($videoId, $title, $description, $cover_url);
                if($rel['code'] != 200){
                    DB::rollback();
                    return array_out(-1006, 'error5');//逻辑错误 - 1005
                }
            }
            //增加审核记录
            $res = model('VideoPlayAiExamine') -> addData($play_id, $video_id='', $examine_status = 0);
            
            //$res1 = model('VideoPlay')->addData($user_id, $video_id, $title, $sub_title ='', $description, $play_type, $is_immerse, $is_rotate, $cover_url, $file_size, $video_classid, $class_tage= '', $is_original);
            
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success');
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    /**
     * 获取视频信息
     * @param user_id int 用户id
     * @param title string  视频标题
     * @param file_name string  视频源文件名

     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function get_create_upload_video_auth($user_id, $title, $file_name){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id'           => $user_id,
                'title'             => $title,
                'file_name'         => $file_name,
            ];
            /********行为参数赋值（*）********/

            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            //添加视频表数据
            $result = controller('Vodlogic', 'logic') -> createUploadVideoRequest($title, $file_name);
            if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, $result['message']);//逻辑错误 - 1005
            }
            //添加阿里云接口（最后处理）
            $video_id = $result['data']['VideoId'];

            //$class_tage = implode(',', $class_tage_array);
            //添加一条数据 状态为3 临时数据
            $res1 = model('VideoPlay')->addData($user_id, $video_id, $title);
            if($res1['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error2');//逻辑错误 - 1005
            }
            $result['data']['playId'] = $res1['data'];//反回playId
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success', (object)$result['data']);
            /********逻辑返回********/

        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    /**
     * 后端使用视频上传-同步
     * @param $user_id
     * @param $title
     * @param $file_name
     * @author dou <51779987@qq.com>
     * @return array
     */
    public function get_create_upload_video_auth_php($user_id, $title, $file_name){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id'           => $user_id,
                'title'             => $title,
                'file_name'         => $file_name,
            ];
            /********行为参数赋值（*）********/

            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            //添加一条数据 状态为4 临时数据
            $res1 = model('VideoPlay')->addData($user_id, $title);
            if($res1['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error2');//逻辑错误 - 1005
            }
            $result['data']['playId'] = $res1['data'];//反回playId
            //上传视频并获取video_id
            $result_video = controller('Vodlogic','logic')->testUploadLocalVideoDemo($file_name);
            if($result_video['code'] < 0){
                DB::rollback();
                return array_out(-1005, $result_video['message']);//逻辑错误 - 1005
            }
            //添加阿里云接口（最后处理）
            $video_id = $result_video['data']['VideoId'];
            //添加一条数据 状态为4 临时数据
            $res2 = model('VideoPlay')->where(['id'=>$result['data']['playId']])->update(['video_id'=>$video_id]);
            if($res2['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error3');//逻辑错误 - 1005
            }

            //$class_tage = implode(',', $class_tage_array);
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
            DB::commit();
            return array_out(200, 'success', (object)$result['data']);
            /********逻辑返回********/

        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }
    /**
     * 后端使用视频上传-异步
     * @param $user_id
     * @param $title
     * @param $file_name
     * @author dou <51779987@qq.com>
     * @return array
     */
    public function get_create_upload_video_auth_php_yi($user_id, $title, $file_name){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id'           => $user_id,
                'title'             => $title,
                'file_name'         => $file_name,
            ];
            /********行为参数赋值（*）********/

            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            //添加一条数据 状态为4 临时数据
            $res1 = model('VideoPlay')->addData($user_id, $title);
            if($res1['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error2');//逻辑错误 - 1005
            }
            $result['data']['playId'] = $res1['data'];//反回playId
            //上传视频并获取video_id
            $result_video = controller('Vodlogic','logic')->testUploadLocalVideo($file_name);
            //异步上传视频
            if($result_video > 0){
                /*$url = $_SERVER['HTTP_HOST'].url('publics/video_curl');
                $url = str_replace('admin.php', 'index.php', $url);
                $body = json_encode($result_video, true);*/
               // trace($body, 'input_params');
                //$url = request()->domain().'/muxiaopu/public/index.php/video/publics/video_curl';
                //$url = request()->domain().'/index.php/video/publics/video_curl';
                $url = 'https://appapi.mushop.live/public/index.php/video/publics/video_curl';
                $curl_param = [
                    'video_id' => $result_video['data'],
                    'file_name' => $file_name,
                ];
                useCurl($url,$curl_param);
            }
            //dump($curl_param);die;
            if($result_video['code'] < 0){
                DB::rollback();
                return array_out(-1005, $result_video['message']);//逻辑错误 - 1005
            }
            //添加阿里云接口（最后处理）
            $video_id = $result_video['data'];
            //添加一条数据 状态为4 临时数据
            $res2 = model('VideoPlay')->where(['id'=>$result['data']['playId']])->update(['video_id'=>$video_id]);
            if($res2['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error3');//逻辑错误 - 1005
            }

            //$class_tage = implode(',', $class_tage_array);
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
            DB::commit();
            return array_out(200, 'success', (object)$result['data']);
            /********逻辑返回********/

        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    /**
     * 获取视频信息
     * @param user_id int 用户id
     * @param video_id string  视频标识
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function get_refresh_upload_video_request($user_id, $video_id){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id'       => $user_id,
                'video_id'      => $video_id,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            //添加视频表数据
            $result = controller('Vodlogic', 'logic') -> refreshUploadVideoRequest($video_id); 
            if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, $result['message']);//逻辑错误 - 1005
            }
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success', (object)$result['data']);
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    	
    }

    /**
     * 视频上传成功
     * @param VideoId string 视频标识
     * @param FileUrl string  资源地址
     * @param EventTime string  UTC上传成功时间
     * @param Size longblob  文件大小
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function file_upload_complete($VideoId, $FileUrl, $EventTime, $Size){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'VideoId'        => $VideoId,
                'FileUrl'        => $FileUrl,
                'EventTime'      => $EventTime,
                'Size'           => $Size,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            //更新 审核表 待审
            $map = [];
            $map['video_id']	= $VideoId;
            model('VideoPlayAiExamine') -> where($map) ->setField('examine_status', 0);

            //更新视频表数据
            $map = [];
            $map['video_id']	= $VideoId;
            $data = [];
            $data['file_url'] = $FileUrl;

            $data['file_size'] = $Size;
            $data['play_operation_status'] = 0;//视频进入待审(旧)
            //$data['play_operation_status'] = 4;//视频进入临时池子(新)
            $data['upload_success_time'] = strtotime($EventTime);
            $res1 = model('VideoPlay')->where($map)->update($data);//dump($res1);

            //是否 开启智能审核
            /* if(config('upload_AI_media_audit_job')){
                //智能AI审核
                $rel = controller('Vodlogic', 'logic') -> submitAIMediaAuditJob($VideoId);
            }else{
                ##是否开启 人工审核
                if(config('ai_examine_artificial_config') == 0){//不需要智能审核  也不需要人工审核
                    $this -> updatePalyStatusNormal($VideoId);
                    //更新作品数
                    $map = [];
                    $map['video_id'] = $VideoId;
                    $uid = model('VideoPlay') -> where($map) -> find() -> uid;
                    model('user_center/UcMemberCount', 'model\user') -> incWorkTimes($uid);
                }
            }  */

            
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success');
            /********逻辑返回********/
			
        } catch (\Exception $e) {
            DB::rollback();
            //file_put_contents('video21112.text', $e -> getMessage().PHP_EOL, FILE_APPEND);
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }

    }

    /**
     * 视频上传成功
     * @param VideoId string 视频标识
     * @param FileUrl string  资源地址
     * @param EventTime string  UTC上传成功时间
     * @param Size longblob  文件大小
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function file_upload_complete_xin($VideoId, $FileUrl, $EventTime, $Size){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'VideoId'        => $VideoId,
                'FileUrl'        => $FileUrl,
                'EventTime'      => $EventTime,
                'Size'           => $Size,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            //更新 审核表 待审
            $map = [];
            $map['video_id']	= $VideoId;
            model('VideoPlayAiExamine') -> where($map) ->setField('examine_status', 0);

            //更新视频表数据
            $map = [];
            $map['video_id']	= $VideoId;
            $data = [];
            $data['file_url'] = $FileUrl;
            $data['file_size'] = $Size;
            $data['play_operation_status'] = 0;//视频进入临时池子(新)
            $data['upload_success_time'] = strtotime($EventTime);
            $res1 = model('VideoPlay')->where($map)->update($data);//dump($res1);
            
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success');
            /********逻辑返回********/
			
        } catch (\Exception $e) {
            DB::rollback();
            //file_put_contents('video21112.text', $e -> getMessage().PHP_EOL, FILE_APPEND);
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }

    }

    /**
     * 智能审核成功
     * @param VideoId string 视频标识
     * @param EventTime string  UTC 成功时间
     * @param Status string  作业状态。取值：success(成功)，fail(失败)
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function ai_media_audit_complete($VideoId, $EventTime, $Status){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'VideoId'        => $VideoId,
                'EventTime'      => $EventTime,
                'Status'         => $Status,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            if(config('ai_examine_artificial_config') == 1){//开启人工审核
                //更新审核表数据
                $map = [];
                $map['video_id']	= $VideoId;
                $data = [];
                $data['examine_status'] = $Status == 'success' ? 2 : 3;
                $data['examine_time'] = strtotime($EventTime);
                $res1 = model('VideoPlayAiExamine')->where($map)->update($data);//dump($res1);
            }else{//关闭人工审核
                if($Status == 'success'){
                    //更新审核表数据
                    $map = [];
                    $map['video_id']	= $VideoId;
                    $data = [];
                    $data['examine_status'] = $Status == 'success' ? 2 : 3;
                    $data['examine_time'] = strtotime($EventTime);
                    $res1 = model('VideoPlayAiExamine')->where($map)->update($data);//dump($res1);
                    //更新视频表状态为 正常
                    $this -> updatePalyStatusNormal($VideoId);
                    
                    //更新作品数
                    $map = [];
                    $map['video_id'] = $VideoId;
                    $uid = model('VideoPlay') -> where($map) -> find() -> uid;
                    model('user_center/UcMemberCount', 'model\user') -> incWorkTimes($uid);
                }
                
            }
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success');
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    /**
     * 人工审核成功
     * @param VideoId string 视频标识
     * @param EventTime string  UTC 成功时间
     * @param Status string  作业状态。取值：success(成功)，fail(失败)
     * @param AuditStatus string  取值范围 Normal(通过)，Blocked(屏蔽)。
     * @param Auditor string  操作用户
     * @param Reason string  理由
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function create_audit_complete($VideoId, $EventTime, $Status, $AuditStatus, $Auditor, $Reason){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'VideoId'        => $VideoId,
                'EventTime'      => $EventTime,
                'Status'         => $Status,
                'AuditStatus'    => $AuditStatus,
                'Auditor'        => $Auditor,
                'Reason'         => $Reason,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            //更新审核表数据
            $map = [];
            $map['video_id']	= $VideoId;
            $data = [];
            $data['artificial_status']  = $AuditStatus == 'Normal' ? 1 : 2;
            $data['artificial_time']    = strtotime($EventTime);
            $data['artificial_auditor'] = $Auditor;
            $data['artificial_reason']  = $Reason;//dump($VideoId);
            $res1 = model('VideoPlayAiExamine')->where($map)->update($data);
            //更新状态
            $this -> updatePalyStatusNormal($VideoId);
            
            //触发转码
            $videoid = $VideoId;
            $templateid = '909c5d139ba29c0972143c2720b561db';
            $rel = controller('Vodlogic', 'logic') -> submitTranscodeJobs($videoid, $templateid);
            $videoid = $VideoId;
            $templateid = '4d5660a9136b3a8188f7b24ca61d7ae9';
            $rel = controller('Vodlogic', 'logic') -> submitTranscodeJobs($videoid, $templateid);

            
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success');
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    	
    }

    /**
     * 视频合成完成
     * @param VideoId string 视频标识
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function produceMediaComplete($VideoId){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'VideoId'        => $VideoId,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            //更新表数据为正常
            $map = [];
            $map['video_id'] = $VideoId;
            model('VideoPlay') -> where($map)-> setField('play_operation_status', $play_operation_status = 1);

            //触发转码
            $templateid = '909c5d139ba29c0972143c2720b561db';
            $rel = controller('Vodlogic', 'logic') -> submitTranscodeJobs($VideoId, $templateid);
            $templateid = '4d5660a9136b3a8188f7b24ca61d7ae9';
            $rel = controller('Vodlogic', 'logic') -> submitTranscodeJobs($VideoId, $templateid);

            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success');
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    	
    }

    /**
     * 获取视频播放凭证
     * @param user_id int 用户id
     * @param play_id int 视频id
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function get_video_play_auth($user_id, $play_id){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id'      => $user_id,
                'play_id'      => $play_id,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $videoId = model('VideoPlay') -> find($play_id) -> video_id;
            //获取凭证
            $result = controller('Vodlogic', 'logic') -> getVideoPlayAuth($videoId);
            if($result['code'] < 0){
                DB::rollback();
                return array_out(-1005, $result['message']);//逻辑错误 - 1005
            }
            $data['playAuth'] = $result['data']['PlayAuth'];
            $data['videoId'] = $videoId;
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success', $data);
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    /**
     * 更新 作品状态为 正常 和 缓存池操作
     * @param VideoId string 视频标识
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function updatePalyStatusNormal($VideoId){
        //更新视频表 正常
        $map = [];
        $map['video_id']	= $VideoId;
        model('video/VideoPlay') -> where($map)-> setField('play_operation_status', 1);

        //更新《用户视频列表池》缓存
        $map = [];
        $map['video_id']	= $VideoId;
        $VideoPlayInfo = model('video/VideoPlay') -> where($map) -> find();
        $uid = $VideoPlayInfo->uid;
        controller('video/VideoPlay', 'datacache') -> getListByUid($uid, $limit = 500, $cache = true);//更新用户视频缓存池
    }
   
}